package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"os"
	"time"
)

func main() {
	router := gin.Default()

	// 给表单限制上传大小 (默认 32 MiB)
	//router.MaxMultipartMemory = 8 << 20 // 8 MiB
	router.POST("/upload", Upload)

	router.POST("/uploadMore", UploadMore)

	router.Run()
}

func UploadMore(c *gin.Context)  {
	form, _ := c.MultipartForm()
	files, _ := form.File["upload[]"]

	for _, file := range files {
		log.Println(file.Filename)

		// 上传文件到指定的路径
		path, _ := os.Getwd()
		dst := fmt.Sprintf("%s/upload/%d.jpg", path, time.Now().UnixNano())
		_ = c.SaveUploadedFile(file, dst)
	}

	c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files)))
}


func Upload(c *gin.Context)  {
	//单文件
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	// 上传文件到指定的路径
	path, _ := os.Getwd()
	dst := fmt.Sprintf("%s/upload/%d.jpg", path, time.Now().UnixNano())
	_ = c.SaveUploadedFile(file, dst)

	c.String(200, fmt.Sprintf("%s uploaded", file.Filename))

}
